/**
 * Copyright 2016 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

goog.module('historian.historianV2Logs');
goog.module.declareLegacyNamespace();


/**
 * The CSV data, the name of the log source it was constructed from,
 * and optionally the start time of the log.
 * @typedef {{source: !Sources, csv: string, startMs: (number|undefined)}}
 */
exports.Log;


/**
 * The types of log sources.
 *
 * @enum {string}
 */
var Sources = {
  BATTERY_HISTORY: 'Battery History',
  BROADCASTS_LOG: 'Broadcasts',
  EVENT_LOG: 'Event',
  KERNEL_DMESG: 'Kernel Dmesg',
  KERNEL_TRACE: 'Kernel Trace',
  LAST_LOGCAT: 'Last Logcat',
  POWER_MONITOR: 'Power Monitor',
  SYSTEM_LOG: 'System',
  WEARABLE: 'Wearable',

  // Data generated by Historian v2 on the JS side. e.g. KERNEL_UPTIME.
  GENERATED: 'Generated',

  // Custom defined groups.
  CUSTOM: 'Custom Group',

  // Headings displayed in the timeline, for describing a set of groups.
  HEADING: 'Heading'
};


/**
 * Time range between min and max, in unix milliseconds.
 * @typedef {{min: number, max: number}}
 */
var Extent;


/**
 * Returns the extent that covers the time range of all the specified logs.
 * @param {!Object<!Extent>} logToExtent Map from log to start and end times.
 * @param {!Array<Sources>} logs Logs to get full extent for.
 * @return {?Extent} The extent covering all the logs, null if no logs
 *     had corresponding extents in the logToExtent map.
 */
exports.getExtent = function(logToExtent, logs) {
  return logs.reduce(function(maxExtent, log) {
    var curExtent = logToExtent[log];
    return curExtent && maxExtent ? {
      min: Math.min(curExtent.min, maxExtent.min),
      max: Math.max(curExtent.max, maxExtent.max)
    } : curExtent || maxExtent;
  }, null);
};


/**
 * The types of log sources.
 *
 * @enum {string}
 */
exports.Sources = Sources;


/** @typedef {{min: number, max: number}} */
exports.Extent = Extent;
